package irri.soils.ssnm;

import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class ActivityV extends SSNM implements TextWatcher{
	private Bundle b;
	private EditText orgBags;
	private EditText orgWt;
	private boolean prompted = false;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.v);
        setCustomTitle();
        TextView question1 = (TextView) findViewById(R.id.question1);
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        textView1.setText(get("organic4"));
        TextView textView2 = (TextView) findViewById(R.id.textView2);
        textView2.setText(get("organic6"));
        b = this.getIntent().getExtras();
        double ha = b.getDouble("ha");
        
        question1.setText(""+get("organic3")+" "+df.format(ha)+" "+get("oorganic11")+"?");
        
        orgBags = (EditText) findViewById(R.id.orgBags);
        orgWt = (EditText) findViewById(R.id.orgWt);
        
        orgBags.addTextChangedListener(this);
        orgWt.addTextChangedListener(this);
        
        if(b.containsKey("orgbag")){
        	orgBags.setText(""+b.getInt("orgBags"));
        	orgWt.setText(""+b.getInt("orgWt"));
        }

        mInputView = (KeyboardView) findViewById(R.id.keyboard);
        
        mInputView.setOnKeyboardActionListener(this);
        mSymbolsKeyboard = new LatinKeyboard(this, R.xml.number);
        mInputView.setKeyboard(mSymbolsKeyboard);
        
    }
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(ActivityV.this, ActivityR.class);
		intent.putExtras(b);
		startActivity(intent);
		finish();
	}

	@Override
	public void afterTextChanged(Editable s) {
		String bag = ""+orgBags.getText();
		String wt = ""+orgWt.getText();
		int orgBagsVal = bag.equals("") ? 0 : Integer.parseInt(bag);
		int orgWtVal = wt.equals("") ? 0 : Integer.parseInt(wt);
		if(orgBagsVal>999){ 
			orgBags.setText(""); 
			orgBags.append(bag.substring(0, bag.length()-1));
		}
		if(orgWtVal>=60&&!prompted){
			displayToast(get("check"));
			prompted = true;
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		
	}
	
	/* (non-Javadoc)
	 * @see irri.soils.ssnm.SSNMActivity#onKey(int, int[])
	 */
	@Override
	public void onKey(int primaryCode, int[] keyCodes) {
		View v = this.getCurrentFocus();
		try{
			TextView tv = (TextView) v;
			String str = ""+tv.getText();
			
			if (primaryCode == Keyboard.KEYCODE_DELETE) {
				onDelete(tv, str);
			}
			else if(primaryCode == 10){
				if(tv.getId()==orgBags.getId()) orgWt.requestFocus();
				else{
				Intent intent = new Intent(ActivityV.this, ActivityW.class);
				try{
					int orgBagsVal = Integer.parseInt(""+orgBags.getText());
					int orgWtVal = Integer.parseInt(""+orgWt.getText());
					if(orgWtVal<5||orgWtVal>70){
						displayToast(get("check"));
						return;
					}
					if(orgBagsVal>0){
					b.putInt("orgBags", orgBagsVal);
					b.putInt("orgWt", orgWtVal);
					intent.putExtras(b);
					startActivity(intent);
					finish();
					}
				}catch(Exception ex){
				}
				}
			}
			else{
				onDigit(tv, str, primaryCode);
			}
		}catch(Exception ex){
			
		}
	}
}